package com.wimcorp.magic.abilities;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.wimcorp.magic.abilities.trigger.AbilityTrigger;
import com.wimcorp.magic.cards.Creature;
import com.wimcorp.magic.exception.CastException;
import com.wimcorp.magic.input.Input;
import com.wimcorp.magic.input.Target;

/**
 * example {input : 'SINGLE'}
 * 
 * 
 * input : SINGLE,MULTI
 */
@Entity
@Table(name = "comboability", schema = "abilities")
public class ComboAbility extends Ability {

	@OneToMany(cascade = CascadeType.ALL)
	private List<Ability> abilities = new ArrayList<>();

	@Enumerated(EnumType.STRING)
	private Input inputOption;

	protected ComboAbility() {
	}

	public ComboAbility(AbilityTrigger trigger, List<Ability> abilites, Input inputOption) {
		super(trigger);
		this.abilities = abilites;
		this.inputOption = inputOption;
	}

	public ComboAbility(ComboAbility ability) {
		super(ability);
		List<Ability> abilities = ability.getAbilities();
		for(Ability a : abilities){
			a.setCard(ability.getCard());
			this.abilities.add(a.clone());
		}
		this.inputOption = ability.getInputOption();
	}

	public void cast() throws Exception {
		switch (inputOption) {
		case SINGLE:
			executeAbility();
			break;
		case MULTI:
			for (Ability ability : abilities) {
				ability.getPlayer().cast(ability);
			}
			break;
		}

	}
	
	

	public void uncast() {
		for (Ability ability : abilities) {
			ability.uncast();
		}
	}

	@Override
	protected void executeAbility() throws CastException {
		for (Ability ability : abilities) {
			ability.executeAbility();
		}

	}

	public Input getInputOption() {
		return inputOption;
	}

	@Override
	public Ability clone() {
		return new ComboAbility(this);
	}

	public List<Ability> getAbilities() {
		return abilities;
	}
	
	public void configure() throws RemoteException{
		switch (inputOption) {
		case SINGLE:
			if(abilities.size() > 0){
				//print description for other abilities
				for(int i = 1; i < abilities.size(); i++){
					getPlayer().message(abilities.get(i).getCastDescription());
				}
				abilities.get(0).configure();
				for (Ability ability : abilities) {
					ability.setAbilityTarget(abilities.get(0).getAbilityTarget());
				}
			}
			break;
		case MULTI:
			for (Ability ability : abilities) {
				ability.configure();
			}
			break;
		}
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder("		id : " + getIdentifier());
		for (Ability ability : abilities) {
			sb.append("\n");
			sb.append("		" + ability.getDescription());
			sb.append("\n");
		}
		return sb.toString();
	}
	
	@Override
	public String getCastDescription() {
		StringBuilder sb = new StringBuilder();
		for (Ability ability : abilities) {
			sb.append("		" + ability.getDescription());
			sb.append("\n");
		}
		return sb.toString();
	}
	
	@Override
	public boolean isInputRequired() {
		for (Ability ability : abilities) {
			if(ability != null && ability.isInputRequired()){
				return true;
			}
		}
		return false;

	}


}
